package cn.cqautotest.cqviehelper.ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat;

import android.os.Bundle;

import com.hjq.base.BaseDialog;

import java.util.concurrent.Executor;

import cn.cqautotest.cqviehelper.R;
import cn.cqautotest.cqviehelper.base.BaseActivity;
import cn.cqautotest.cqviehelper.ui.dialog.ToastDialog;
import cn.cqautotest.cqviehelper.utils.LogUtils;
import cn.cqautotest.cqviehelper.utils.StringJoinUtils;

/**
 * 测试类
 */
public class TestActivity extends BaseActivity {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_test;
    }

    @Override
    protected void initData() {
        startHomeActivity();
    }

    private void startHomeActivity() {
        boolean isSupport = checkAuthenticatorsTypeSupport();
        LogUtils.d(this, StringJoinUtils.join("is support ====>", isSupport));
        if (!isSupport) {
            new ToastDialog.Builder(TestActivity.this).setMessage("此设备不支持生物识别").create().show();
            return;
        }
        Executor executor = ContextCompat.getMainExecutor(this);
        BiometricPrompt biometricPrompt = new BiometricPrompt(this, executor, new BiometricPrompt.AuthenticationCallback() {
            @Override
            public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
                super.onAuthenticationSucceeded(result);
                BaseDialog tipsDialog = new ToastDialog.Builder(TestActivity.this).setMessage("身份验证通过").create();
                tipsDialog.show();
                tipsDialog.addOnDismissListener(dialog -> {
                    startActivity(HomeActivity.class);
                    finish();
                });
            }

            @Override
            public void onAuthenticationFailed() {
                super.onAuthenticationFailed();
                new ToastDialog.Builder(TestActivity.this).setMessage("身份验证失败").create().show();
            }

            @Override
            public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
                super.onAuthenticationError(errorCode, errString);
                new ToastDialog.Builder(TestActivity.this).setMessage(errString).create().show();
            }
        });
        BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
                .setTitle("生物识别认证")
                .setDescription("我们需要通过您的生物信息来验证您的真实身份")
                .setNegativeButtonText("使用密码验证")
                .build();
        biometricPrompt.authenticate(promptInfo);
    }

    private boolean checkAuthenticatorsTypeSupport() {
        BiometricManager biometricManager = BiometricManager.from(this);
        int canAuthenticate = biometricManager.canAuthenticate();
        return canAuthenticate == BiometricManager.BIOMETRIC_SUCCESS;
    }
}